<!--
 * new page
 * @author: liuxin
 * @since: 2024-08-16
 * delete_admin.vue
-->
<template>
	<el-dialog v-model="dialogFormVisible" title='审核产品' width="30%" center>
		<div class="discribe">确定审核此产品的出库吗？</div>
		<div class="my-2 ml-4">
			<el-radio-group v-model="formData.product_out_status">
				<el-radio value="同意" size="large">同意</el-radio>
				<el-radio value="否决" size="large">否决</el-radio>
			</el-radio-group>
			<el-input type="textarea" aria-placeholder="请输入审核备注" v-model="formData.audit_memo" />
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="auditProductFn">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onBeforeUnmount } from "vue"
import { bus } from "@/utils/mitt.js"
import { tracking } from "@/utils/operation.js"
import { auditProduct } from '@/api/product.js'
import { ElMessage } from 'element-plus'
import { useUserInfor } from '@/store/userinfor.js'

const userStore = useUserInfor()

interface FormData {
	id: number | null,
	product_out_id: number | null,
	product_out_status: string,
	audit_memo: string,
	product_out_price: number | null,
	product_out_audit_person: string,
	product_out_apply_person: string,
	product_inwarehouse_number: number | null,
	product_single_price: number | null,
	product_out_number: string
}

const formData = reactive({
	id: 0,
	product_out_id: 0,
	product_name: '',
	product_out_status: '',
	audit_memo: '',
	product_out_price: 0,
	product_out_audit_person: userStore.name,
	product_out_apply_person: '',
	product_inwarehouse_number: 0,
	product_single_price: 0,
	product_out_number: '',
})
const auditProductId = ref()
bus.on('auditProductInfor', (row: any) => {
	formData.id = row.id
	formData.product_out_id = row.product_out_id
	formData.product_name = row.product_name
	formData.audit_memo = row.audit_memo
	formData.product_out_status = row.product_out_status
	formData.product_out_price = row.product_out_price
	formData.product_out_apply_person = row.product_out_apply_person
	formData.product_inwarehouse_number = row.product_inwarehouse_number
	formData.product_single_price = row.product_single_price
	formData.product_out_number = row.product_out_number
})
const emit = defineEmits(['success'])
// 删除产品

const auditProductFn = async () => {
	const res = await auditProduct(formData)
	// console.log(formData);
	if (res.status == 0) {
		ElMessage({
			message: '审核产品成功',
			type: 'success'
		})
		tracking('产品', localStorage.getItem('name'), formData.product_name, '高级', formData.product_out_status)
		emit('success')
		dialogFormVisible.value = false
	} else {
		ElMessage.error('审核产品失败')
		dialogFormVisible.value = false
	}
}


// 弹窗开关

const dialogFormVisible = ref(false)
// 打开编辑管理员弹窗
const open = () => {
	dialogFormVisible.value = true
}
defineExpose({
	open
})

// 取消订阅/监听
onBeforeUnmount(() => {
	bus.all.clear()
})
</script>

<style scoped lang="scss">
.discribe {
	margin-bottom: 8px;
}
</style>